package com.yunao.service.report;

import com.yunao.commons.serachcore.core.bean.Article;
import com.yunao.entity.ArticleItem;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


public class ExcelWriterItem {

	private Workbook wb;
	private OutputStream out;
	private List<String[]> list;
	private final String[] strs = new String[] { "", "标题", "作者", "来源", "发布时间", "链接", "媒体类型", "正负面", "频道分类", "网站",
			"网站级别", "作者类型", "信息类型", "地区",  "摘要", "正文" };

	public ExcelWriterItem(List<ArticleItem> articles, OutputStream out) {
		this.wb = new HSSFWorkbook();
		this.out = out;
		this.list = parse(articles);
	}

	/*public ExcelWriter(SearchBeans beans, OutputStream out) {
		this.wb = new HSSFWorkbook();
		this.out = out;
		this.list = parse(beans);
	}*/

	/**
	 * 导出 excel
	 */
	public boolean write() {
		if (list == null || list.size() == 0)
			return false;
		Sheet sheet = wb.createSheet();
		// 导航
		this.createHeader(sheet, 0, strs);
		CellStyle linkstyle=getLinkedStyle(wb);
		CellStyle normalstyle=getNormalStyle(wb);
		for (int i = 0; i < list.size(); i++) {
			// 正文
			this.createNormal(sheet, (i + 1), list.get(i),normalstyle,linkstyle);
		}
		try {
			wb.write(out);
		} catch (Exception e) {

		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {

				}
		}
		return true;
	}

	/**
	 * 导航
	 */
	public void createHeader(Sheet sheet, int rowNum, String[] strs) {
		Row row = sheet.createRow(rowNum);
		for (int i = 0; i < strs.length; i++) {
			Cell cell = row.createCell(i);
			cell.setCellStyle(getHeaderStyle(wb));
			cell.setCellValue(strs[i]);
		}
	}

	/**
	 * 正文
	 */
	public void createNormal(Sheet sheet, int rowNum, String[] strs,CellStyle normalstyle,CellStyle linkstyle) {
		Row row = sheet.createRow(rowNum);
		
		for (int i = 0; i < strs.length; i++) {
			Cell cell = row.createCell(i);
			if (strs.length == 20 && i == 5)
				cell.setCellStyle(linkstyle);
			else
				cell.setCellStyle(normalstyle);
			cell.setCellValue(strs[i]);
		}
	}

	/**
	 * 转换
	 */
	private List<String[]> parse(List<ArticleItem> articles) {
		if (articles == null || articles.size() == 0)
			return null;
		List<String[]> list = new ArrayList<String[]>();
		for (ArticleItem article : articles) {
			String[] strs = new String[20];
			strs[0] = parse(article.getArticleId()+"");
			strs[1] = parse(article.getArticleTitle());
			strs[2] = parse(article.getArticleAuthor());
			strs[3] = parse(article.getArticleDomainSource());
			strs[4] = parse(article.getArticlePubdate()+"");
			strs[5] = parse(article.getArticleUrl());
			strs[6] = parse(article.getArticleMediaType());
			strs[7] = parse(article.getArticleEmotion());
			strs[8] = parse(article.getArticleChannel());
//			strs[9] = parse(article.getChannel());
			strs[9] = parse(article.getArticleDomain());
			strs[10] = parse(article.getArticleDomainLevel());
			strs[11] = parse("");
			strs[12] = parse(article.getArticleMessageType());
			strs[13] = parse(article.getArticleLocaltion());
			strs[14] = parse(article.getArticleSunmary());
			
			if(article.getArticleText()!=null&&article.getArticleText().length()<32760)
			strs[15] = parse(article.getArticleText());
			else if(article.getArticleText()!=null)
				strs[15] = parse(article.getArticleText().substring(0,32756)+"...");
			else
				strs[15] = parse(article.getArticleText());
			list.add(strs);
		}
		return list;
	}

	/**
	 * 字符串非空
	 */
	private String parse(String str) {
		return (str == null || str.length() == 0) ? "" : str.trim();
	}

	/**
	 * 顶部样式
	 */
	private static CellStyle getHeaderStyle(Workbook wb) {
		CellStyle style = wb.createCellStyle();
		Font font = wb.createFont();
		font.setFontName("微软雅黑");
		font.setColor(HSSFColor.BLACK.index);
		font.setFontHeight((short) 240);
		style.setFont(font);
		style.setBorderBottom((short) 1);
		return style;
	}

	/**
	 * 正文样式
	 */
	private static CellStyle getNormalStyle(Workbook wb) {
		CellStyle style = wb.createCellStyle();
		return style;
	}

	/**
	 * 链接样式
	 */
	private static CellStyle getLinkedStyle(Workbook wb) {
		CellStyle style = wb.createCellStyle();
		Font font = wb.createFont();
		font.setFontName(HSSFFont.FONT_ARIAL);
		font.setUnderline((byte) 1);
		font.setColor(HSSFColor.BLUE.index);
		style.setFont(font);
		return style;
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		/*DbUtil db = DbUtil.getInstance();
		List<Article> articles = (List<Article>) db.query(1, 10, "FROM Article");
		System.out.println(articles.size());
		String filepath = "C:\\Users\\glt\\Desktop\\" + System.currentTimeMillis() + ".xls";
		File file = new File(filepath);
		if (!file.exists())
			file.createNewFile();
		OutputStream out = new FileOutputStream(file);
		ExcelWriter writer = new ExcelWriter(articles, out);
		boolean flag = writer.write();
		System.out.println(flag);*/
	}

}
